'use strict';
const db = uniCloud.database()
const {
	verifyToken
} = require('wx-common')

exports.main = async (event, context) => {
	const {
		action,
		data
	} = event

	switch (action) {
		case 'wechatLogin':
			return await wechatLogin(event)
		case 'getUserProfile':
			return await getUserProfile(event)
		case 'updateUserProfile':
			return await updateUserProfile(event)
		case 'updateHeight':
			return await updateHeight(event)
		case 'updatecsweight':
			return await updatecsweight(event)
		case 'updatembweight':
			return await updatembweight(event)
		case 'getuser':
			return await getuser(event)
		default:
			return {
				code: -1,
					msg: '未知操作'
			}
	}
}
// 获取用户信息
async function getuser(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}
		// 先查询用户信息
		const userInfo = await db.collection('user').where({
			openid: decoded.value
		}).get()
		if (userInfo.data.length === 0) {
			return {
				code: -1,
				msg: '用户不存在'
			}
		}
		const $ = db.command.aggregate
		const count = await db.collection('weight_records').where({
			openid: decoded.value
		}).count()
		if(count.total==0){
			return {
				code: 0,
				msg: '获取成功',
				data: {
					...userInfo.data[0],
					count: 0,
					avg: 0,
					maxweight: 0,
					minweight: 0
				}
			}
		}else{
			const avg = await db.collection('weight_records').aggregate().match({
				openid: decoded.value
			}).group({
				_id: null,
				average: $.avg('$weight')
			}).end()
			const max = await db.collection('weight_records').aggregate().match({
				openid: decoded.value
			}).group({
				_id: null,
				maxweight: $.max('$weight')
			}).end()
			const min = await db.collection('weight_records').aggregate().match({
				openid: decoded.value
			}).group({
				_id: null,
				minweight: $.min('$weight')
			}).end()
			return {
				code: 0,
				msg: '获取成功',
				data: {
					...userInfo.data[0],
					count: count.total,
					avg: avg.data[0].average.toFixed(2),
					maxweight: max.data[0].maxweight.toFixed(2),
					minweight: min.data[0].minweight.toFixed(2)
				}
			}
		}
		
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 更新用户信息
async function updateUserProfile(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		// 先查询用户信息
		const userInfo = await db.collection('user').where({
			openid: decoded.value
		}).get()
		if (userInfo.data.length === 0) {
			return {
				code: -1,
				msg: '用户不存在'
			}
		}

		// 更新用户信息
		await db.collection('user').doc(userInfo.data[0]._id).update(data)

		return {
			code: 0,
			msg: '更新成功'
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 更新目标体重
async function updatembweight(event) {

	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		const {
			mbweight
		} = data

		// 先查询用户信息
		const userInfo = await db.collection('user').where({
			openid: decoded.value
		}).get()
		if (userInfo.data.length === 0) {
			return {
				code: -1,
				msg: '用户不存在'
			}
		}
		if (userInfo.data[0].mb) {
			const update = userInfo.data[0].mb.update
			const nowdate = new Date().getFullYear() + '-' + new Date().getMonth()
			const jldate = new Date(update).getFullYear() + '-' + new Date(update).getMonth()
			if (nowdate == jldate) {
				return {
					code: -2,
					msg: '每个月只能设置一次目标体重'
				}
			}
		}
		// 更新用户目标体重
		await db.collection('user').doc(userInfo.data[0]._id).update({
			mb: {
				mbweight,
				update: new Date()
			}
		})

		return {
			code: 0,
			msg: '更新成功'
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 更新用户身高
async function updateHeight(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		const {
			height
		} = data

		// 先查询用户信息
		const userInfo = await db.collection('user').where({
			openid: decoded.value
		}).get()
		if (userInfo.data.length === 0) {
			return {
				code: -1,
				msg: '用户不存在'
			}
		}

		// 更新用户身高
		await db.collection('user').doc(userInfo.data[0]._id).update({
			height
		})

		return {
			code: 0,
			msg: '更新成功'
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 更新初始体重
async function updatecsweight(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		const {
			csweight
		} = data

		// 先查询用户信息
		const userInfo = await db.collection('user').where({
			openid: decoded.value
		}).get()
		if (userInfo.data.length === 0) {
			return {
				code: -1,
				msg: '用户不存在'
			}
		}
		if (userInfo.data[0].cs) {
			const update = userInfo.data[0].cs.update
			const nowdate = new Date().getFullYear() + '-' + new Date().getMonth()
			const jldate = new Date(update).getFullYear() + '-' + new Date(update).getMonth()
			if (nowdate == jldate) {
				return {
					code: -2,
					msg: '每个月只能设置一次初始体重'
				}
			}
		}
		// 更新用户初始体重
		await db.collection('user').doc(userInfo.data[0]._id).update({
			cs:{
				csweight,
				update:new Date()
			}
		})

		return {
			code: 0,
			msg: '更新成功'
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}